﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DevComponents.DotNetBar.Controls;
using System.Data;

namespace BUL
{
    public partial class Business
    {
        readonly QuanLyBanHangEntities1 _dal = new QuanLyBanHangEntities1();
        public void GetAllKho(string strTimkiem, DataGridViewX dataGridViewX )
        {
            if(dataGridViewX.RowCount>0)
                dataGridViewX.Rows.Clear();
            using (var dal = new QuanLyBanHangEntities1())
            {
                var listResult = dal.tblKhoes.Where(x => x.TenKho.Contains(strTimkiem)).OrderBy(x => x.TenKho);
                int i = 0;
                foreach (var item in listResult)
                {
                    dataGridViewX.Rows.Add();
                    dataGridViewX.Rows[i].Cells[0].Value = "N";
                    dataGridViewX.Rows[i].Cells[1].Value = item.MaKho;
                    dataGridViewX.Rows[i].Cells[2].Value = item.TenKho;
                    dataGridViewX.Rows[i].Cells[3].Value = item.DienThoai;
                    dataGridViewX.Rows[i].Cells[4].Value = item.DiaChi;
                    dataGridViewX.Rows[i].Cells[5].Value = item.LienHe;
                    dataGridViewX.Rows[i].Cells[6].Value = item.GhiChu;
                    i++;
                }
            }
        }


        /// <summary>
        /// Lay ma kho
        /// </summary>
        /// <param name="makho"></param>
        /// <returns></returns>
        public tblKho GetKhoById(string makho)
        {
            return _dal.tblKhoes.Where(x => x.MaKho == makho).FirstOrDefault();
        }

        public void InsertKho(tblKho kho)
        {
            //kiểm tra mã kho đã tồn tại
            //kiểm tra độ lớn của txt nhập vào
            _dal.tblKhoes.AddObject(kho);
            _dal.SaveChanges();
        }

        public void UpdateKho(string makhoCu,tblKho kho)
        {
            var khoUpdate = _dal.tblKhoes.Where(x => x.MaKho == makhoCu).FirstOrDefault();
            khoUpdate.MaKho = kho.MaKho;
            khoUpdate.TenKho = kho.TenKho;
            khoUpdate.DiaChi = kho.DiaChi;
            khoUpdate.DienThoai = kho.DienThoai;
            khoUpdate.LienHe = kho.LienHe;
            khoUpdate.GhiChu = kho.GhiChu;
            _dal.SaveChanges();
        }

        public void DeleteKho(IEnumerable<string> maKhos)
        {
            var listDelete = _dal.tblKhoes.Where(x => maKhos.Contains(x.MaKho));
            foreach (var tblKho in listDelete)
            {
                _dal.tblKhoes.DeleteObject(tblKho);
            }

            _dal.SaveChanges();
        }

        public tblKho BuildKho(string makho, string tenkho,string dienthoai,string diachi, string lienhe,string ghichu)
        {
            return new tblKho {MaKho=makho,TenKho=tenkho,DienThoai = dienthoai,DiaChi = diachi,LienHe = lienhe,GhiChu = ghichu};
        }
    }
}
